#!/bin/sh

# Slackware build script for netbeans 
# Written by core (eroc@linuxmail.org)
# Modified by Robby Workman

# This script is just a binary repackaging.
# You can make a localised package with this SlackBuild 
# (default is english). For example:
#
# To get netbeans in Spanish - Espa�ol (es) :
# 1).- Go to: http://www.netbeans.info/downloads/all.php?b_id=3095
# 2).- Under "Archive Distributions", download the package:
#      netbeans-5_5_1-es.zip
# 3).- Execute the slackbuild as:
#      NBLANG=es ./netbeans.SlackBuild
#      to finally get in /tmp: netbeans-5.5.1_es-i586-1_SBo.tgz
#
# Other values for NBLANG could be: ja, pt_BR, zh_CN, de and zh_TW
#
# This SlackBuild was tested with "es" and "pt_BR" and with default
# language (English) so if it works for you with the other values,
# it's a good idea to send feedback about it :)

set -e

PRGNAM=netbeans
VERSION=5.5.1
VERSIONALT='5_5_1'
ARCH=i586 # Leave this alone
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION

# If NBLANG has a value:
if [ ! -z $NBLANG ]; then
    NBLANGSRC="-$NBLANG" # For the sourcecode
    NBLANGPKG="_$NBLANG" # For the final package
    NBLANGLOC=$(echo "--locale $NBLANG" | sed 's/_/:/') # To netbeans's locale.
fi

unzip $CWD/$PRGNAM-$VERSIONALT${NBLANGSRC}.zip || exit 1
mv $PRGNAM $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

mkdir $PKG/opt
cp -R $TMP/$PRGNAM-$VERSION $PKG/opt

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# Add netbeans to KDE/GNOME/XFCE menu
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# Add an icon for netbeans
mkdir -p $PKG/usr/share/pixmaps
cp $TMP/$PRGNAM-$VERSION/nb5.5/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# Add a script to call netbeans from usr/bin.
mkdir -p $PKG/usr/bin
cat <<- EOF > $PKG/usr/bin/$PRGNAM
#!/bin/sh
cd /opt/$PRGNAM-$VERSION/bin
./$PRGNAM $NBLANGLOC
EOF
chmod 0755 $PKG/usr/bin/$PRGNAM

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a build_info CREDITS* DISTRIBUTION.txt LICENSE.txt \
  netbeans.css README* THIRDPARTYLICENSE.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n -p $OUTPUT/$PRGNAM-$VERSION$NBLANGPKG-$ARCH-$BUILD$TAG.tgz
